package ru.spb.osll.myfit.client.widget;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;

/**
 * Its a class for "imitating" a progress bar.
 * ProgressBar is represented by 2 buttons in an AbsolutePanel.
 * By different "changing" functions this buttons will change its width parameters.
 * It will be like the ProgressBar updates.
 * @author Savelyev Dan.
 *
 */

public class ProgressBarWidget extends Grid {
  private Button m_buttonLeft;
  private Button m_buttonRight;
  private Label m_stateLabel;  
  private int m_progressBarWidth;
  private int m_totalWidth;
  private int m_currentState;
  
  public ProgressBarWidget () {
    super(1,2);
    this.setWidth("200");
    m_totalWidth = 100;    
    m_progressBarWidth = m_totalWidth - 30;    
    defineProgressBar();
  }
  
  public ProgressBarWidget (int width) {
    super(1,2);
    this.setWidth(""+width);
    m_totalWidth = 100;    
    m_progressBarWidth = m_totalWidth - 30;    
    defineProgressBar();
  }
  
  private void defineProgressBar() {
    this.setHeight("100%");
        
    AbsolutePanel panel = new AbsolutePanel();
    m_stateLabel = new Label();
    m_buttonLeft = new Button();
    m_buttonRight = new Button();
    
    this.setWidget(0,0,panel);
    this.setWidget(0,1,m_stateLabel);
  
    panel.add(m_buttonLeft);
    panel.add(m_buttonRight);
        
    m_currentState = 0;
    
    panel.setWidth(m_progressBarWidth+"%");
    
    m_stateLabel.setText(m_currentState+"%");
    m_stateLabel.setHeight("100%");
    
    m_buttonLeft.setEnabled(false); 
    m_buttonLeft.setWidth("1%");
    m_buttonLeft.setVisible(false);
    m_buttonLeft.setHeight("100%");
        
    m_buttonRight.setEnabled(false);
    m_buttonRight.setWidth("99%");
    m_buttonRight.setHeight("100%");
  }
  
  /**
   * 
   * @param progressSegments - current ("finished work") number of segments of the progress bar. 
   * @param totalSegments - total number of segments of the progress bar.
   */
  public void update (int progressSegments, int totalSegments) {
    if (progressSegments>totalSegments) {return;}
    if (m_currentState > (progressSegments * 100 / totalSegments)) {return;}
    int progressPercent, restPercent, totalPercent;
    totalPercent = 100;
    progressPercent = (totalPercent / totalSegments) * progressSegments;
    restPercent = totalPercent - progressPercent;
    if (progressPercent<15) {
      m_buttonLeft.setWidth("1%");
      m_buttonLeft.setVisible(false);
    } else {
      m_buttonLeft.setWidth(""+(progressPercent-1)+"%");
      m_buttonLeft.setVisible(true);      
    }
    if (restPercent<15) {
      m_buttonRight.setWidth("1%");
      m_buttonRight.setVisible(false);
    } else {
      m_buttonRight.setWidth(""+restPercent+"%");
      m_buttonRight.setVisible(true);      
    }
    updateCurrentState (progressSegments, totalSegments);    
  }
  
  private void updateCurrentState (int progressSegments, int totalSegments) {    
    m_currentState = progressSegments * 100 / totalSegments;
    m_stateLabel.setText(m_currentState+"%");
  }
  
  public void reset() {
    m_currentState = 0;
    
    m_stateLabel.setText(m_currentState+"%");
    
    m_buttonLeft.setWidth("1%");
    m_buttonLeft.setVisible(false);
        
    m_buttonRight.setWidth("100%");   
    m_buttonRight.setVisible(true);
  }
  
}